package com.changgou.business.listener;


import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Map;

@Component
public class AdListener {
    @Autowired
    private RestTemplate restTemplate;
    //从mq中取消息,指定队列
    @RabbitListener(queues = "ad_update_queue")
    public void getMessage(String message){
        System.out.println("接收到的消息是"+message);
        /*String url="http://192.168.200.128/ad_update?position="+message;
        String s = JSON.toJSONString(url);
        restTemplate.getForObject(s, Map.class);*/


        //远程请求lua执行更新
        //第一步 实例化httpClient
        OkHttpClient okHttpClient = new OkHttpClient();
        //第四步设置请求lua 地址
        String url="http://192.168.200.128/ad_update?position="+message;
        //第三步，获取请求对象
        Request request = new Request.Builder().url(url).build();
        //第二步，执行请求
        Call call = okHttpClient.newCall(request);
        //第五步，返回响应
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求成功:"+response.message());
            }
        });

    }
}
